package cn.kust.clock.service.impl;

import cn.kust.clock.mapper.*;
import cn.kust.clock.pojo.vo.ClockDetailVO;
import cn.kust.clock.pojo.vo.ClockVO;
import cn.kust.clock.pojo.dto.ClockDTO;
import cn.kust.clock.pojo.entity.Clock;
import cn.kust.clock.pojo.entity.Image;
import cn.kust.clock.pojo.entity.Video;

import cn.kust.clock.service.ClockService;
import cn.kust.microtourismcommon.pojo.dto.UserIdDTO;
import cn.kust.microtourismcommon.remote.ServiceUserCenterClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author bjs
 * @date 2022/7/20 0020  20:24
 **/
@Slf4j
@Service
public class ClockServiceImpl implements ClockService {
    @Autowired
    private ServiceUserCenterClient userClient;

    @Autowired
    private ClockMapper clockMapper;

    @Autowired
    private ImageMapper imageMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CollectionMapper collectionMapper;

    /**
     *
     * @param page 页数
     * @return 微记列表
     */
    @Override
    public List<ClockVO> clockList(Integer page) throws Exception {
        //1.调用likeMapper获取idlist
        List<Long> idList = likeMapper.selectHotOfHighLike(1, page);
        List<ClockVO> clockVOs = new ArrayList<>();
        //2.获取整个列表对应的微记的列表
        List<Clock> clocks = clockMapper.selectBatchIds(idList);
        //2.1创建一个DTO对象，用来存放userId列表
        UserIdDTO userIdDTO = new UserIdDTO();
        List<Long> userIdList =new ArrayList<>();
        //2.2将userId遍历出来
        for (Clock clock : clocks) {
            userIdList.add(clock.getUserId());
        }
        userIdDTO.setUserIdList(userIdList);
        Object data = userClient.getUserNameAvatarList(userIdDTO).getData();
        //3.循环整个Clocks去转化为对应的VO实体

        HashMap<String,Object> rule = new HashMap<>();
        for (Clock clock : clocks) {
            int i = 0;
            rule.put("clock_sort_id",clock.getId());
            //3.1获取当前微记一张图片作为背景
            List<Image> images = imageMapper.selectByMap(rule);
            //3.2获取当前微记的点赞数
            Integer likeNum = likeMapper.getLikeCountByTypeAndTypeId(1, clock.getId());
            //3.3创建对应VO对象
            ClockVO clockVO = new ClockVO(clock,((List<?>) data).get(i++),images.get(0),likeNum);
            //3.4将VO对象加入VO数组中
            clockVOs.add(clockVO);
            //3.5将map中数据清空
            rule.clear();
        }
        return clockVOs;
    }

    /**
     *
     * @param id 微记的id
     * @return 微记详细信息
     */
    @Override
    public ClockDetailVO clock(Long id) throws Exception {
        //1.查询对应的微记
        Clock clock = clockMapper.selectById(id);
        //2.远程调用得到用户信息
        Object data = userClient.getUserNameAvatarById(clock.getUserId()).getData();
        //3.获取微记对应的图片列表
        HashMap<String,Object> rule = new HashMap<>();
        rule.put("clock_sort_id",id);
        List<Image> images = imageMapper.selectByMap(rule);
        //4.获取对应的视频
        List<Video> videos = videoMapper.selectByMap(rule);
        //5.获取对应的点赞数
        Integer likeNum = likeMapper.getLikeCountByTypeAndTypeId(1, id);
        //6.获取评论总数
        Integer commentNum = commentMapper.getCommentCountByClockId(id);
        //7.将这些数据封装成一个VO
        //8.将对象返回给controller
        return new ClockDetailVO(clock,data,images,videos,likeNum,commentNum);
    }

    /**
     *
     * @param userId 用户id
     * @return 对应用户的微记列表
     */
    @Override
    public List<ClockVO> myselfClockList(Long userId)  throws Exception{
        //1.获取对应userid的用户信息
        Object data = userClient.getUserNameAvatarById(userId).getData();
        System.out.println(data.toString());
        //2.写查询规则
        HashMap<String,Object> rule = new HashMap<>();
        rule.put("user_id",userId);
        //3.通过条件去查询的对应的数据
        List<Clock> clockList = clockMapper.selectByMap(rule);
        //4.创建List<ClockVO>
        List<ClockVO> clockVOList = new ArrayList<>();
        HashMap<String,Object> rule1 = new HashMap<>();
        //5.循环整个Clocks去转化为对应的VO实体
        for (Clock clock : clockList) {
            rule1.put("clock_sort_id",clock.getId());
            //5.1获取当前微记一张图片作为背景
            List<Image> images = imageMapper.selectByMap(rule1);
            //5.2获取当前微记的点赞数
            Integer likeNum = likeMapper.getLikeCountByTypeAndTypeId(1, clock.getId());
            //5.3创建对应VO对象
            ClockVO clockVO = new ClockVO(clock,data,images.get(0),likeNum);
            //5.4将VO对象加入VO数组中
            clockVOList.add(clockVO);
            //5.5将map中数据清空
            rule1.clear();
        }
        return clockVOList;
    }

    /**
     *
     * @param clockDTO 客户端传过来的form表单对象
     * @return 受影响的行数
     */
    @Override
    public int addClock(ClockDTO clockDTO,String position) throws Exception {
        //1.通过传过来的form表单创建微记对象
        Clock clock = new Clock(clockDTO,position,LocalDateTime.now());
        //2.通过mapper去增加数据
        int insert = clockMapper.insert(clock);
        //3.imageMapper 去增加图片数据
        for (String url : clockDTO.getImageUrl()) {
            Image image = new Image(url, clock.getId());
            imageMapper.insert(image);
        }
        //4.videoMapper 去增加视频资源
        for (String url : clockDTO.getVideoUrl()) {
            Video video  = new Video(url, clock.getId());
            videoMapper.insert(video);
        }
        return insert;
    }

    /**
     *
     * @param userId 用户id
     * @return
     * @throws Exception
     */
    @Override
    public List<ClockVO> collectClockList(Long userId) throws Exception {
        //1.调用collectionMapper获取idlist
        List<Long> idList = collectionMapper.selectClockIdListByUserIdAndType(userId);
        List<ClockVO> clockVOs = new ArrayList<>();
        //2.获取整个列表对应的微记的列表
        List<Clock> clocks = clockMapper.selectBatchIds(idList);
        //2.1创建一个DTO对象，用来存放userId列表
        UserIdDTO userIdDTO = new UserIdDTO();
        List<Long> userIdList =new ArrayList<>();
        //2.2将userId遍历出来
        for (Clock clock : clocks) {
            userIdList.add(clock.getUserId());
        }
        userIdDTO.setUserIdList(userIdList);
        Object data = userClient.getUserNameAvatarList(userIdDTO).getData();
        //3.循环整个Clocks去转化为对应的VO实体

        //3.循环整个Clocks去转化为对应的VO实体
        for (Clock clock : clocks) {
            int i = 0;
            HashMap<String,Object> rule = new HashMap<>();
            rule.put("clock_sort_id",clock.getId());
            //3.0获取用户信息
            /*UserNameAvatar user = (UserNameAvatar) userClient.getUserById(clock.getUserId()).getData();*/
            //3.1获取当前微记一张图片作为背景
            List<Image> images = imageMapper.selectByMap(rule);
            //3.2获取当前微记的点赞数
            Integer likeNum = likeMapper.getLikeCountByTypeAndTypeId(1, clock.getId());
            //3.3创建对应VO对象
            ClockVO clockVO = new ClockVO(clock,((List<?>)data).get(i++),images.get(0),likeNum);
            //3.4将VO对象加入VO数组中
            clockVOs.add(clockVO);
            //3.5将map中数据清空
            rule.clear();
        }
        return clockVOs;
    }
}
